﻿<%@ WebHandler Language="C#" Class="Upload" %>

using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;

public class Upload : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Expires = -1;
        try
        {
            HttpPostedFile postedFile = context.Request.Files["Filedata"];
            
            string savepath = "";
            string tempPath = "";
            tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"]; 
            savepath = context.Server.MapPath(tempPath);
            string filename = postedFile.FileName;
            if (!Directory.Exists(savepath))
                Directory.CreateDirectory(savepath);

            postedFile.SaveAs(savepath + @"\" + filename);
            context.Response.Write(tempPath + "/" + filename);
            context.Response.StatusCode = 200;
            
            
            //Save to Database
            System.Drawing.Image imageToBeResized = System.Drawing.Image.FromFile(savepath + @"\" + filename);
            List<Bitmap> bitmapList = new List<Bitmap>();
            bitmapList = ResizeImage(imageToBeResized);
            for (int i = 0; i < bitmapList.Count - 1; i++)
            { 
            
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                bitmapList[i].Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                stream.Position = 0;
                byte[] image = new byte[stream.Length + 1];
                stream.Read(image, 0, image.Length);
                DataAccessLayer dal = new DataAccessLayer();

                int intLength = image.Length;
                
                if (dal.Doc2SQLServer(filename, image, intLength, postedFile.ContentType) == true)
                {
                    //lblStatus.Text = "Image uploaded successfully.";
                }
                else
                {
                    //lblStatus.Text = "An error occured while uploading Image... Please try again.";
                }
            }
            
            
        }
        catch (Exception ex)
        {
            context.Response.Write("Error: " + ex.Message);
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }


    public List<Bitmap> ResizeImage(System.Drawing.Image imageToResize)
    {
        //Resize Large Images
        List<Bitmap> imageList = new List<Bitmap>();
        imageList.Add(ImageGenerator(150, 150, imageToResize));
        
        //ResizeImage to Thumbnail
        imageList.Add(ImageGenerator(20, 30, imageToResize));

        return imageList;                
    }

    public Bitmap ImageGenerator(int reqMaxWidth, int reqMaxHeight, System.Drawing.Image imageToResize)
    {
        int imageHeight = imageToResize.Height;
        int imageWidth = imageToResize.Width;
        int maxHeight = reqMaxHeight;
        int maxWidth = reqMaxWidth;
        imageHeight = (imageHeight * maxWidth) / imageWidth;
        imageWidth = maxWidth;

        if (imageHeight > maxHeight)
        {
            imageWidth = (imageWidth * maxHeight) / imageHeight;
            imageHeight = maxHeight;
        }

        Bitmap bitmap = new Bitmap(imageToResize, imageWidth, imageHeight);
        return bitmap;
    }
}